#include <iostream>

#include "CSignal.h"

using namespace std;

class CSIGHUPHandler : public CSignalHandler {
public :
	CSIGHUPHandler() {};
	~CSIGHUPHandler() {};

	virtual int handle(int signum, siginfo_t *si, void *sv) {
		assert(signum==SIGHUP);

		CSignal::instance()->remove_handler(SIGQUIT);
	};
};

class CSIGINTHandler : public CSignalHandler {
public :
	CSIGINTHandler() {};
	~CSIGINTHandler() {};

	virtual int handle(int signum, siginfo_t *si, void *sv) {
		assert(signum==SIGINT);

		clean_up();

		exit(1);
	};

	void clean_up() {
		cerr << "clean up" << endl;
	};
};

class CSIGQUITHandler : public CSignalHandler {
public :
	CSIGQUITHandler() {};
	~CSIGQUITHandler() {};

	virtual int handle(int signum, siginfo_t *si, void *sv) {
		assert(signum==SIGQUIT);

		cerr << "oops~ aborted" << endl;

		_exit(1);
	};
};

int do_work()
{
	while(1) {
		sleep(1);
		cerr << "hahaha" << endl;
	}
}

int main(void)
{
	CSIGHUPHandler sighup_handler;
	CSIGINTHandler sigint_handler;
	CSIGQUITHandler sigquit_handler;

	CSignal::instance()->register_handler(SIGHUP, &sighup_handler);
	CSignal::instance()->register_handler(SIGINT, &sigint_handler);
	CSignal::instance()->register_handler(SIGQUIT, &sigquit_handler);

	do_work();

	cerr << "never reached" << endl;
}
